import { t } from 'i18next'
import { useMeetingTVWallStore } from '../store'
import shallow from 'zustand/shallow'
import { Modal } from '@kedacom/react-next'
import { PollEntity } from '../interface'
import { SinglePollData } from '../../../components/SinglePollModal'
export const useBoxViewHook = () => {
	const { operate_correct_style_info, correct_style, correct_style_operate_box_info, setOperteUsedStyleMapHduId } =
		useMeetingTVWallStore(
			(state) => ({
				update: state.update,
				correct_style: state.correct_style,
				config: state.config,
				style_map: state.style_map,
				show_mt_name: state.show_mt_name,
				operate_correct_style_info: state.operate_correct_style_info,
				correct_style_operate_box_info: state.correct_style_operate_box_info,
				setOperteUsedStyleMapHduId: state.setOperteUsedStyleMapHduId,
			}),
			shallow
		)
	/** 轮询终端已选择个数 */
	const getPollMembersNum = () => {
		// return operateBoxInfo?.poll.members.length || 0
	}
	/**得到本通道之外的轮询终端*/
	const getMtPollmap = () => {
		/**获取所有的盒子的信息*/
		let _source = operate_correct_style_info[correct_style]?.members ?? []
		let pollMap: { [key: string]: Array<PollEntity> } = {}
		/**过滤出设置了单通道轮询*/
		let mccTVWallPolls = _source.filter(
			(item) => item.member_type === 5 && item.hdu_id !== correct_style_operate_box_info
		)
		/**获取本通道设置已设置的所有终端*/
		for (let i = 0; i < mccTVWallPolls.length; i++) {
			let current = mccTVWallPolls[i]
			let poll = current.poll
			if (poll) {
				let pollMts = poll.members || [] // 轮询成员列表
				// 以终端的mt_key作为件键  终端信息作为value值
				for (let k = 0; k < pollMts.length; k++) {
					let key = `${pollMts[k].account_type}$$${pollMts[k].account}`
					pollMap[key] ? pollMap[key].push(poll) : (pollMap[key] = [poll])
				}
			}
		}
		return pollMap
	}
	/**判断选择终端是否在其他通道当中已设置*/
	const handleConfirmSingle = (single_poll_source: SinglePollData) => {
		/**其他盒子设置的单通道轮询终端 */
		let mtPollMap = getMtPollmap()
		/**当前单通道轮询将要设置单通道轮询成员*/
		let mts = single_poll_source.poll?.members || []
		let confictMt: Array<string> = []
		mts.forEach((item) => {
			let _key = `${item.account_type}$$${item.account}`
			if (mtPollMap[_key] && mtPollMap[_key].length > 0) {
				confictMt.push(item.name || item.account) /**添加进入冲突终端 */
			}
		})
		if (confictMt.length > 0) {
			let _name = ''
			if (confictMt.length <= 4) {
				_name = confictMt.join(',')
			} else {
				_name = confictMt.slice(0, 4).join(',') + '...'
			}
			let m = Modal.confirm({
				content: t('{{name}}在其他通道中已设置轮询，是否重复设置？', { name: _name }),
				onOk: () => {
					m.hide()
					if (!correct_style_operate_box_info) return
					setOperteUsedStyleMapHduId(correct_style, {
						member_type: 5,
						hdu_id: correct_style_operate_box_info,
						poll: single_poll_source.poll,
					})
				},
				onCancel: () => {
					m.hide()
				},
			})
		} else {
			/**改变单通道轮询 */
			if (!correct_style_operate_box_info) return
			setOperteUsedStyleMapHduId(correct_style, {
				member_type: 5,
				hdu_id: correct_style_operate_box_info,
				poll: single_poll_source.poll,
			})

			/**证明单通道轮询数据存在*/
			// const _members = boxDetailVOsSource
			// 	.map((item) => {
			// 		if (item.hdu_id === operateBoxInfo.hdu_id) {
			// 			return {
			// 				member_type: 5,
			// 				hdu_id: operateBoxInfo.hdu_id,
			// 				poll: _poll.poll ? revert_poll(_poll.poll) : null,
			// 			}
			// 		}
			// 		return null
			// 	})
			// 	.filter(Boolean)
			// /**更改单通道轮询 */
			// changeTVWallChannel({
			// 	mode: 1,
			// 	show_mt_name: show_mt_name,
			// 	batch_poll: null,
			// 	members: _members,
			// })
		}
	}
	return {
		/** 格子轮询终端已选择个数 */
		getPollMembersNum,
		/**检测单通道轮询冲突 */
		handleConfirmSingle,
	}
}
